package com.ssm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.ssm.entity.EnterpriseEntity;
import com.ssm.entity.InvitationEntity;
import com.ssm.entity.JobEntity;
import com.ssm.entity.RecruitEntity;
import com.ssm.entity.ResumeEntityWithBLOBs;
import com.ssm.entity.StudentEntity;
import com.ssm.service.EnterpriseService;
import com.ssm.service.InvitationService;
import com.ssm.service.JobService;
import com.ssm.service.RecruitService;
import com.ssm.service.ResumeService;
import com.ssm.service.StudentService;

import myUtils.Response;
import myUtils.Utils;
import myUtils.myPage;
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    @Qualifier("EnterpriseServiceImpl")
    private EnterpriseService enterpriseService;
    
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;
    
    @Autowired
    @Qualifier("JobServiceImpl")
    private JobService jobService;
    
    @Autowired
    @Qualifier("ResumeServiceImpl")
    private ResumeService resumeService;
    
    @Autowired
    @Qualifier("RecruitServiceImpl")
    private RecruitService recruitService;
    
    @Autowired
    @Qualifier("InvitationServiceImpl")
    private InvitationService invitationService;
    
	@RequestMapping(value="/info", method=RequestMethod.POST)
	public Response fillInfo(
		@ModelAttribute StudentEntity stuInfo
	) {
		stuInfo.setStuId(Utils.getUUID());
		studentService.save(stuInfo);
		return Response.successWrap();
	}
	
	/**
	 * 获取学生信息
	 * @param stuId 学生ID
	 * @return
	 */
	@RequestMapping(value= "/info/{id}", method=RequestMethod.GET)
	public Response getInfo(
	  @PathVariable("id")String stuId
	) {
		System.out.println(stuId);
		StudentEntity student = studentService.findById(stuId);
		return Response.successWrap(student);
	}
	
	/**
	 * 分页获取公司详情
	 * @param page 页数
	 * @param pageNum 每页的数量
	 * @return
	 */
	@RequestMapping(value= "/companys", method=RequestMethod.GET)
	public Response getCompanys(
		@RequestParam(value="page",required=false,defaultValue="1")Integer page,
		@RequestParam(value="pageNum",required=false, defaultValue= "10")Integer pageNum,
		@RequestParam(value="name",required=false)String name,
		@RequestParam(value="field",required=false)Integer field
		
	) {
		List<EnterpriseEntity> companyList = enterpriseService.findWithPage(page, pageNum, name, field);
		return Response.successWrap(companyList, myPage.pageInfo(companyList));
	}
	
	/**
	 * 根据ID获取公司详情
	 * @return
	 */
	@RequestMapping(value= "/company/{id}", method=RequestMethod.GET)
	public Response getCompany(
	  @PathVariable("id")String companyId
	) {
		EnterpriseEntity company = enterpriseService.findById(companyId);
		return Response.successWrap(company);
	}
	
	/**
	 * 获取所有的岗位
	 * @param page 页数
	 * @param pageNum 每页的数量
	 * @return
	 */
	@RequestMapping(value= "/jobs", method=RequestMethod.GET)
	public Response getJobs(
		@RequestParam(value="page",required=false,defaultValue="1")Integer page,
		@RequestParam(value="pageNum",required=false, defaultValue= "10")Integer pageNum,
		@RequestParam(value="coId",required=false)String coId
	) {
		List<JobEntity> jobList = jobService.findWithPage(page, pageNum, coId);
		return Response.successWrap(jobList, myPage.pageInfo(jobList));
	}
	
	/**
	 * 根据岗位ID获取岗位详细信息
	 * @param page 页数
	 * @param pageNum 每页的数量
	 * @return
	 */
	@RequestMapping(value= "/job/{id}", method=RequestMethod.GET)
	public Response getJob(
		@PathVariable("id")String jobId
	) {
		JobEntity job = jobService.findById(jobId);
		return Response.successWrap(job);
	}
	
	/**
	 * 根据名字搜索公司，模糊搜索
	 * @return
	 */
	@RequestMapping(value= "/company/search", method=RequestMethod.GET)
	public Response searchCompanyByName(
	  @RequestParam(value="name",required=true,defaultValue="")String name
	) {
		List<EnterpriseEntity> companys = enterpriseService.searchByName(name);
		return Response.successWrap(companys, myPage.pageInfo(companys));
	}
	
	/**
	 * 根据分类搜索公司，精确搜索
	 * @return
	 */
	@RequestMapping(value= "/company/searchField", method=RequestMethod.GET)
	public Response searchCompanyByField(
	  @RequestParam(value="field",required=true,defaultValue="")Integer field
	) {
		System.out.print(field);
		List<EnterpriseEntity> companys = enterpriseService.searchByField(field);
		return Response.successWrap(companys, myPage.pageInfo(companys));
	}
	/*-----3.8更新-------------------*/
	/**
	 * 更新或者修改简历
	 * @return
	 */
	@RequestMapping(value= "/resume", method=RequestMethod.POST)
	public Response updateResume(
	  @ModelAttribute ResumeEntityWithBLOBs resume
	) {
		String resumeId = resume.getResumeId();
		System.out.println(resumeId);
		if (resumeId == null || resumeId == "") {
			resume.setResumeId(Utils.getUUID());
			resumeService.save(resume);
		} else {
			resumeService.update(resume);
		}
		return Response.successWrap();
	}
	
	/**
	 * 根据学生ID获取简历信息
	 * @return
	 */
	@RequestMapping(value= "/resume/{stuId}", method=RequestMethod.GET)
	public Response getResumeByStu(
		@PathVariable("stuId")String stuId
	) {
		ResumeEntityWithBLOBs resume = resumeService.findByStudent(stuId);
		return Response.successWrap(resume);
	}
	
	
	/**
	 * 创建应聘信息
	 * @return
	 */
	@RequestMapping(value= "/recruit", method=RequestMethod.POST)
	public Response applyJob(
	  @ModelAttribute RecruitEntity recruit
	) {
		recruit.setRecTime(new Date());
		// 设置应聘状态为接受中
//		recruit.setRecStatus(0);
		recruitService.save(recruit);
		return Response.successWrap();
	}
	
	/**
	 * 分页获取学生的应聘记录
	 * @return
	 */
	@RequestMapping(value= "/recruit", method=RequestMethod.GET)
	public Response allRecruit(
		@RequestParam(value="stuId",required=true)String stuId,
		@RequestParam(value="page",required=false,defaultValue="1")Integer page,
		@RequestParam(value="pageNum",required=false, defaultValue= "10")Integer pageNum
	) {
		List<RecruitEntity> recruits = recruitService.findWithPageByStudent(stuId, page, pageNum);
		return Response.successWrap(recruits, myPage.pageInfo(recruits));
	}
	
	/**
	 * 分页获取学生的被公司邀请记录
	 * @return
	 */
	@RequestMapping(value= "/invitation", method=RequestMethod.GET)
	public Response allInvitation(
		@RequestParam(value="stuId",required=true)String stuId,
		@RequestParam(value="page",required=false,defaultValue="1")Integer page,
		@RequestParam(value="pageNum",required=false, defaultValue= "10")Integer pageNum
	) {
		List<InvitationEntity> invitations = invitationService.findWithPageByStudent(stuId, page, pageNum);
		return Response.successWrap(invitations, myPage.pageInfo(invitations));
	}
	
	/**
	 * 更新邀请涵状态
	 * @return
	 */
	@RequestMapping(value= "/updteInvitation/{id}", method=RequestMethod.POST)
	public Response updateInvitation(
	  @PathVariable("id")String invitationId,
	  @RequestParam(value="status",required=true,defaultValue="0")Integer status
	) {
		InvitationEntity invitation = new InvitationEntity();
		invitation.setStatus(status);
		invitation.setInvitationId(invitationId);
		Boolean result = invitationService.update(invitation);
		if (result) {
			return Response.successWrap();
		} else {
			return Response.failWrap("修改邀请涵状态失败");
		}
		
	}
	
	@RequestMapping(value="/uploadFile", method=RequestMethod.POST)
	public Response uploadFile(
		HttpServletRequest request,
		@RequestParam(value="file", required=true) MultipartFile file
	) {
		String filePath = request.getSession().getServletContext().getRealPath("/upload/file");
		System.out.println(filePath);
		String fileName = Utils.uploadFileHandler(filePath, file);
		
		if (fileName != null) { return Response.successWrap(fileName); }
		else {
			return Response.failWrap("上传文件失败");
		}
	}

}
